package test.transferFile3;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Io流处理线程
 * 
 * @author Simple
 * @date 2013-6-24 下午12:27:09
 * @Description TODO
 */
public class IoStreamThreadWork extends Thread {

  public static final int BUFFER_SIZE=1024 * 4;

  private BufferedInputStream bis;

  private BufferedOutputStream bos;

  public void setBis(BufferedInputStream bis) {
    this.bis=bis;
  }

  public BufferedInputStream getBis() {
    return bis;
  }

  public void setBos(BufferedOutputStream bos) {
    this.bos=bos;
  }

  public BufferedOutputStream getBos() {
    return bos;
  }

  public IoStreamThreadWork(InputStream in, OutputStream out) {
    bis=new BufferedInputStream(in);
    bos=new BufferedOutputStream(out);
  }

  // 写数据
  @Override
  public void run() {
    byte[] bufferByte=new byte[BUFFER_SIZE];
    int tempData=0;
    try {
      while((tempData=bis.read(bufferByte)) != -1) {
        System.out.println("写"+tempData);
        bos.write(bufferByte, 0, tempData);
      }
      bos.flush();
    } catch(IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      try {
        if(bos != null)
          bos.close();
        if(bis != null)
          bis.close();
      } catch(IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
}
